package com.bestcem.xm.wallet.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.om.grpc.v1.enums.EmailConfigTypeEnum;
import com.bestcem.xm.om.grpc.v1.services.GetOmEmailConfigRequest;
import com.bestcem.xm.om.grpc.v1.services.GetOmEmailConfigResponse;
import com.bestcem.xm.om.grpc.v1.services.UserServiceGrpc;
import com.bestcem.xm.wallet.enums.InvoiceNotifyEmailTypeEnum;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * om用户相关服务
 *
 * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
 * @version v1.0
 * @date 2022/07/12 10:36
 */
@Slf4j
@Service("walletOmUserGrpcService")
public class WalletOmUserGrpcService {

    @GrpcClient(GrpcClientNames.DS_USER)
    private UserServiceGrpc.UserServiceBlockingStub stub;

    /**
     * 根据通知类型获取通知邮箱配置
     *
     * @param notifyType 通知类型
     * @param emailType  邮件类型
     * @return java.util.List<java.lang.String>
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/07/12 10:38
     */
    public List<String> getOmEmailConfig(Integer notifyType, Integer emailType) {
        if (log.isInfoEnabled()) {
            log.info("根据type查询om配置的邮件列表参数：notifyType:{}，emailType:{}", notifyType, emailType);
        }

        GetOmEmailConfigRequest.Builder builder = GetOmEmailConfigRequest.newBuilder();
        EmailConfigTypeEnum.EmailConfigType emailConfigType = EmailConfigTypeEnum.EmailConfigType.forNumber(notifyType);
        builder.setType(emailConfigType);

        // 获取租户后台配置的通知邮箱
        GetOmEmailConfigResponse omEmailConfig = stub.getOmEmailConfig(builder.build());
        // 校验检查结果
        if (com.bestcem.bp.grpc.v1.base.Code.ERROR_VALUE == omEmailConfig.getStatus().getCodeValue()) {
            log.error("根据key查询om配置的邮件列表失败：dto:{}", omEmailConfig.getStatus().getMessage());
            return Collections.emptyList();
        }

        return InvoiceNotifyEmailTypeEnum.TEST.getType().equals(emailType) ? omEmailConfig.getTestEmailsList() : omEmailConfig.getRealEmailsList();
    }

}
